package com.donggua.aichebao.commonui;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.view.View;
/**
 * Created by Administrator on 2017-08-05.
 */

public class SelectTextLayout extends RelativeLayout {
    private TextView tvSelect;
    private View viLine;
    private int textSizeType = TypedValue.COMPLEX_UNIT_PX;
    private final static int DEFUAL_SIZE = 20;
    private  int lineHeight = 2;
    public SelectTextLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.CustomTextView);
        lineHeight = (int)context.getResources().getDimension(R.dimen.lineHeight);
        tvSelect = getTextView(context);
        String text = ta.getString(R.styleable.CustomTextView_customText);
        int textSize =(int) ta.getDimension(R.styleable.CustomTextView_customTextSize,DEFUAL_SIZE);
        int color = ta.getColor(R.styleable.CustomTextView_customTextColor,0xff000000);
        RelativeLayout.LayoutParams tvSelectLayoutParams = getMyLayoutParams();
        tvSelectLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE);
        tvSelect.setLayoutParams(tvSelectLayoutParams);
        tvSelect.setText(text);
        tvSelect.setTextSize(textSizeType,textSize);
        tvSelect.setTextColor(color);
        this.addView(tvSelect);
        viLine = new View(context);
        viLine.setBackgroundColor(color);

        RelativeLayout.LayoutParams lineLayoutParams = new LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lineLayoutParams.height = lineHeight;
        lineLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
        viLine.setLayoutParams(lineLayoutParams);
        this.addView(viLine);
        ta.recycle();
    }

    public SelectTextLayout(Context context){
        super(context);
    }

    private TextView getTextView(Context context) {
        TextView textView = new TextView(context);
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(textSizeType,DEFUAL_SIZE);
        return textView;
    }

    private static RelativeLayout.LayoutParams getMyLayoutParams(){
        LayoutParams layoutParams = new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        return layoutParams;
    }

    public void setSelectColor(int color){
        tvSelect.setTextColor(color);
        viLine.setBackgroundColor(color);
    }

    public void lineShow(boolean is){
        if(is){
            viLine.setVisibility(View.VISIBLE);
        }else{
            viLine.setVisibility(View.GONE);
        }
    }
}
